﻿using Microsoft.Extensions.DependencyInjection;
using Silky.Core.Configuration;
using Silky.Core;
using Simboss.Csharp.Sdk.Request.Constant;
using Microsoft.Extensions.Configuration;
using Simboss.Csharp.Sdk.Conf;

namespace Caviar.simboss_csharp_sdk
{
    public static class SimbossCollectionExtensions
    {
        public static IServiceCollection SimbossServices(this IServiceCollection services)
        {
            //配置文件
            var setting = EngineContext.Current.Configuration.
                                                   GetSection(SimbossConfig.SimbossConfigSettings);
            if (setting == null)
            {
                return services;
            }
            services.AddOptions<SimbossConfig>()
                    .Bind(setting);

            var op = setting.Get<SimbossConfig>();
            if (op == null)
            {
                throw new ArgumentNullException("Simboss配置文件不能为空");
            }
            // 注入HttpClient
            services.AddHttpClient("simbossclient", config =>
            {
                config.BaseAddress = new Uri(UriConstants.API_URL);
                config.Timeout = TimeSpan.FromMilliseconds(int.Parse(op.optConf["httpTimeout"]));
            });
            services.AddHttpClient();
            services.AddSingleton<ISimbossClient, SimbossClient>();
            return services;
        }
    }
}